<template>
  <!-- 试题录入 -->
  <div class="container">
    <div class="box">
      <div class="title">试题录入</div>
      <div class="box_form">
        <!-- form表单 -->
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="学科" prop="subjectID">
            <el-select v-model="form.subjectID" style="width: 400px">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.subjectName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录" prop="catalogID">
            <el-select v-model="form.catalogID" style="width: 400px">
              <el-option
                v-for="item in option"
                :key="item.id"
                :label="item.subjectName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="企业" required prop="enterpriseID">
            <el-select v-model="form.enterpriseID" style="width: 400px">
              <el-option
                v-for="item in formData"
                :key="item.id"
                :label="item.shortName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="城市" prop="province">
            <el-select
              v-model="form.province"
              placeholder="请选择"
              style="width: 195px"
            >
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
            <span>&nbsp;&nbsp;&nbsp;</span>
            <el-select
              v-model="form.city"
              placeholder="请选择"
              style="width: 194px"
            >
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="方向" prop="direction">
            <el-select
              placeholder="请选择"
              style="width: 400px"
              v-model="form.direction"
            >
              <el-option
                v-for="item in directions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="题型" prop="questionType">
            <el-radio-group v-model="form.questionType">
              <el-radio :label="1">单选</el-radio>
              <el-radio :label="2">多选</el-radio>
              <el-radio :label="3">简答</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="难度" prop="">
            <el-radio-group v-model="form.difficulty">
              <el-radio :label="1">简单</el-radio>
              <el-radio :label="2">一般</el-radio>
              <el-radio :label="3">困难</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="题干" prop="question">
            <el-col :span="23">
              <quill-editor
                v-model="form.question"
                style="height: 200px"
              ></quill-editor>
            </el-col>
          </el-form-item>
          <!-- TODO选项 -->
          <el-form-item label="选项" class="abcd">
            <el-radio-group v-model="form.options">
              <el-radio :label="1"
                >A：
                <el-input v-model="input" />
                <!-- 上传图片 -->
                <!-- <el-upload
                  class="avatar-uploader"
                  action="https://jsonplaceholder.typicode.com/posts/"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload> -->
              </el-radio>
              <br />
              <el-radio :label="2"
                >B：
                <el-input v-model="input" />
              </el-radio>
              <br />
              <el-radio :label="3"
                >C：
                <el-input v-model="input" />
              </el-radio>
              <br />
              <el-radio :label="4"
                >D：
                <el-input v-model="input" />
              </el-radio>
            </el-radio-group>
            <br />
            <el-button type="danger" disabled>+增加选项与答案</el-button>
          </el-form-item>
          <el-form-item label="解析视频">
            <el-input v-model="form.videoURL"></el-input>
          </el-form-item>
          <el-form-item label="答案解析" prop="answer">
            <el-col :span="23">
              <quill-editor
                v-model="form.answer"
                style="height: 200px"
              ></quill-editor>
            </el-col>
          </el-form-item>
          <el-form-item label="题干备注" class="labs">
            <el-col :span="10">
              <el-input
                type="textarea"
                v-model="form.remarks"
                :rows="4"
              ></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="试题标签">
            <el-select
              v-model="form.tags"
              placeholder="请选择活动区域"
              style="width: 380px"
            >
              <el-option
                v-for="item in option"
                :key="item.id"
                :label="item.subjectName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('form')" class="butt"
              >确认提交</el-button
            >
          </el-form-item>
        </el-form>
        <!-- 从基础题库传过来的数据 -->
        <!-- <p>{{ $route.query }}</p> -->
      </div>
    </div>
  </div>
</template>

<script>
// 局部注册
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'

import { list } from '@/api/hmmm/subjects.js'
export default {
  components: {
    quillEditor
  },
  data () {
    return {
      imageUrl: '',
      input: '',
      form: {
        subjectID: '', // 学科
        catalogID: '', // 目录
        enterpriseID: '', // 企业
        direction: '', // 方向
        province: '', // 城市
        city: '', // 地区
        questionType: 1, // 题型  moren
        difficulty: 1, // 难度
        question: '', // 题干
        videoURL: '', // 解析视频
        answer: '', // 答案解析
        remarks: '', // 题目备注
        tags: '', // 标签
        options: [], // 选项
        input: '' // 选项内容
      },
      rules: {
        subjectID: [{ required: true, message: '请选择学科', trigger: 'blur' }],
        catalogID: [{ required: true, message: '请选择目录', trigger: 'blur' }],
        enterpriseID: [
          { required: true, message: '请选择企业', trigger: 'blur' }
        ],
        province: [{ required: true, message: '请选择城市', trigger: 'blur' }],
        direction: [{ required: true, message: '请选择方向', trigger: 'blur' }],
        questionType: [
          { required: true, message: '请选择题型', trigger: 'blur' }
        ],
        difficulty: [
          { required: true, message: '请选择难度', trigger: 'blur' }
        ],
        question: [{ required: true, message: '请输入题干', trigger: 'blur' }],
        answer: [{ required: true, message: '请输入答案', trigger: 'blur' }]
      },
      options: [], // 学科列表
      option: [], // 目录列表，无数据
      formData: [], // 企业列表
      // 方向
      directions: [
        {
          value: '选项1',
          label: 'o2o'
        },
        {
          value: '选项2',
          label: '外包服务'
        },
        {
          value: '选项3',
          label: '企业服务'
        },
        {
          value: '选项4',
          label: '互联网金融'
        },
        {
          value: '选项5',
          label: '企业咨询'
        },
        {
          value: '选项6',
          label: '互联网'
        },
        {
          value: '选项7',
          label: '电子商务'
        },
        {
          value: '选项8',
          label: '其他'
        }
      ],
      queryObj: {
        page: 1,
        pagesize: 20
      }
    }
  },
  created () {
    // this.getSubjectList()
    // this.getCompanysList()
  },
  methods: {
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 上传图片的

    // 封装获取学科列表请求
    async getSubjectList () {
      const { data } = await list(this.queryObj)
      this.options = data.items
    },
    // 确认
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.box {
  width: 1300px;
  background-color: #fff;
  margin: 16px 26px;
  .title {
    width: 100%;
    height: 50px;
    font-size: 16px;
    padding-left: 20px;
    line-height: 50px;
    border-bottom: 1px solid #ccc;
  }
  .box_form {
    width: 100%;
    // height: 1300px;
    padding-left: 20px;
    padding-top: 20px;
    .abcd {
      margin-top: 120px;
      .el-input {
        width: 240px;
        margin-bottom: 20px;
      }
    }
  }
  .el-input {
    width: 400px;
  }
}
.butt {
  margin-bottom: 60px;
}
.labs {
  margin-top: 120px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
